package com.pick.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.pick.admin.entity.SysOperationLog;
import com.pick.admin.service.SysOperationLogService;
import com.pick.base.annotation.OperationLog;
import com.pick.base.common.PageQuery;
import com.pick.base.common.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 系统操作日志控制器
 *
 * @author pick
 * @since 2024-01-01
 */
@Tag(name = "系统操作日志管理")
@RestController
@RequestMapping("/admin/operation-log")
@RequiredArgsConstructor
public class SysOperationLogController {

    private final SysOperationLogService operationLogService;

    /**
     * 分页查询操作日志
     */
    @Operation(summary = "分页查询操作日志")
    @GetMapping("/page")
    @SaCheckPermission("system:operation-log:query")
    public Result<IPage<SysOperationLog>> page(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer current,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "用户名") @RequestParam(required = false) String username,
            @Parameter(description = "模块") @RequestParam(required = false) String module,
            @Parameter(description = "操作类型") @RequestParam(required = false) String operationType,
            @Parameter(description = "状态") @RequestParam(required = false) Integer status) {
        
        // 构造分页查询参数
        PageQuery pageQuery = new PageQuery();
        pageQuery.setCurrent(current.longValue());
        pageQuery.setSize(size.longValue());
        
        // 构造关键词搜索（可以根据需要组合多个查询条件）
        StringBuilder keyword = new StringBuilder();
        if (username != null && !username.trim().isEmpty()) {
            keyword.append("username:").append(username).append(" ");
        }
        if (module != null && !module.trim().isEmpty()) {
            keyword.append("module:").append(module).append(" ");
        }
        if (operationType != null && !operationType.trim().isEmpty()) {
            keyword.append("operationType:").append(operationType).append(" ");
        }
        if (status != null) {
            keyword.append("status:").append(status).append(" ");
        }
        if (keyword.length() > 0) {
            pageQuery.setKeyword(keyword.toString().trim());
        }
        
        IPage<SysOperationLog> page = operationLogService.pageOperationLogs(pageQuery);
        return Result.success(page);
    }

    /**
     * 查看操作日志详情
     */
    @Operation(summary = "查看操作日志详情")
    @GetMapping("/{id}")
    @SaCheckPermission("system:operation-log:query")
    public Result<SysOperationLog> getById(@PathVariable Long id) {
        SysOperationLog log = operationLogService.getById(id);
        return Result.success(log);
    }

    /**
     * 批量删除操作日志
     */
    @Operation(summary = "批量删除操作日志")
    @DeleteMapping("/batch")
    @SaCheckPermission("system:operation-log:delete")
    @OperationLog(module = "系统管理", type = "删除", description = "批量删除操作日志")
    public Result<Void> deleteBatch(@RequestBody java.util.List<Long> ids) {
        operationLogService.removeByIds(ids);
        return Result.success();
    }

    /**
     * 清理过期日志
     */
    @Operation(summary = "清理过期日志")
    @DeleteMapping("/clean")
    @SaCheckPermission("system:operation-log:delete")
    @OperationLog(module = "系统管理", type = "删除", description = "清理过期操作日志")
    public Result<Integer> cleanExpiredLogs(
            @Parameter(description = "保留天数") @RequestParam(defaultValue = "30") Integer days) {
        int count = operationLogService.cleanExpiredLogs(days);
        return Result.success(count);
    }

    /**
     * 导出操作日志
     */
    @Operation(summary = "导出操作日志")
    @GetMapping("/export")
    @SaCheckPermission("system:operation-log:export")
    @OperationLog(module = "系统管理", type = "导出", description = "导出操作日志")
    public Result<String> export(
            @Parameter(description = "用户名") @RequestParam(required = false) String username,
            @Parameter(description = "模块") @RequestParam(required = false) String module,
            @Parameter(description = "操作类型") @RequestParam(required = false) String operationType,
            @Parameter(description = "状态") @RequestParam(required = false) Integer status) {
        
        // TODO: 实现导出功能
        return Result.success("导出功能待实现");
    }
}